/**********
//@Team HotSource
//@author bloodevil<bloodevil4@gmail.com>
***********/
#include "www_data.h"
#include "image_view.h"
#include "ui_image_view.h"
#include "mainwindow.h"

#include <libexif/exif-data.h>
#include <libexif/exif-entry.h>
#include <libexif/exif-ifd.h>
#include <libexif/exif-content.h>

image_view::image_view(QWidget *parent)
    : QDialog(parent), ui(new Ui::image_view)
{
    ui->setupUi(this);
    //image_view::view_image();
}

image_view::~image_view()
{
    delete ui;
}

void image_view::view_image(QString imagepath, QList<QTableWidgetItem*> image_list)
{
    //QImage image("C:/Qt/Hotsource/image_view/1.JPG");
    QImage image(imagepath);
    image_view::OriginalImage = image;
    double factor = (double) (ui->imagelabel->size().width()*4) / image_view::OriginalImage.size().width();
    //double factor_h = (double) (ui->imagelabel->size().height()*5)/image_view::OriginalImage.size().height();
    QImage first(image_view::OriginalImage.scaled(OriginalImage.size()*factor,Qt::KeepAspectRatio, Qt::FastTransformation));
    ui->imagelabel->setPixmap(QPixmap::fromImage(first));
    ui->image_size->setValue((int)(100*factor));
    //widget resize
    resize(first.size().width()+60, first.size().height()+160);
    ui->spinBox->setSuffix("%");
    image_view::exifView(imagepath);

    //load images in image_table
    for(int i = 0; i < image_list.count(); i++)
    {
        QListWidgetItem *item = new QListWidgetItem(image_list.at(i)->icon(), image_list.at(i)->text());
        item->setStatusTip(image_list.at(i)->statusTip());
        ui->listWidget->addItem(item);
        ui->listWidget->setIconSize(QSize::QSize(65,65));
    }
    ui->listWidget->setViewMode(QListView::IconMode);

    //ui->memo_label->setText(ui->listWidget->ststatusTip());

    connect(ui->listWidget, SIGNAL(itemDoubleClicked(QListWidgetItem*)), this, SLOT(choice_image(QListWidgetItem*)));
}


//when people double click the image in image_list_widget, call this.
void image_view::choice_image(QListWidgetItem* imagepath)
{
    QImage image(imagepath->statusTip());
    image_view::OriginalImage = image;
    double factor = (double) (ui->imagelabel->size().width()*1.44) / image_view::OriginalImage.size().width();
    ui->imagelabel->setPixmap(QPixmap::fromImage(image));
    ui->spinBox->setSuffix("%");
}

void image_view::zoom(int SliderValue)
{
    double factor = (double) SliderValue / 100;
    QImage ScaledImage(image_view::OriginalImage.scaled(OriginalImage.size()*factor,Qt::KeepAspectRatio, Qt::FastTransformation));
    ui->imagelabel->setPixmap(QPixmap::fromImage(ScaledImage));
}

void image_view::exifView(QString imagepath)
{
    //please input the right path!!!!!*********************************************
    //QString imagepath = ("/home/hotsource/image_view/1.jpg");

    //QFile f(imagepath);
    QFile f(imagepath);
    QFileInfo fi(imagepath);

    //get exif data
    ExifData *ed = exif_data_new_from_file (imagepath.toLocal8Bit().constData());
    ExifEntry *ee = exif_content_get_entry(ed->ifd[EXIF_IFD_0], EXIF_TAG_MODEL);
    char model[256]={0,};
    exif_entry_get_value(ee,model,sizeof(model));
    QString camera_model(model);

    //save temp
    char date[256]={0,};
    ee = exif_content_get_entry(ed->ifd[EXIF_IFD_0], EXIF_TAG_DATE_TIME);
    exif_entry_get_value(ee, date, sizeof(date));
    QString datetime(date);

    ui->exifinfo->setText(datetime);
    ui->filepath->setText(fi.fileName());
    ui->cameraname->setText(model);
}
